import Vue from 'vue'
const state = {
  // 自定义购物车数据
  cartList: [
    {
      count: 1,
      selected: true,
      product_id: 10000131,
      name: '小米MIX 3 8GB+128GB',
      title: 'DxO百分拍照手机',
      product_desc_ext:
        '4800万拍照千元机 / “满血版”骁龙660处理器 / 品质“小金刚”，18个月超长质保 / 4000mAh 大电量 / 6.3" 水滴全面屏 / USB-C 接口，方便耐用 / 支持快充 4 协议',
      price_max: '2599',
      market_price_max: '2599',
      price_min: '2599',
      market_price_min: '2599',
      value: '8GB+128GB',
      images: [
        'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/411d540271f6efbdd882fa33daee0de3.jpg?thumb=1&w=200&h=200&f=webp&q=90',
      ],
    },
    {
      count: 1,
      selected: true,
      product_id: 10000138,
      name: '小米CC9',
      title: '3200万自拍，4800万三摄',
      product_desc_ext:
        '「3GB+32GB、4GB+64GB，直降100，到手价低至699元」4000mAh大电量 / 骁龙632八核处理器 / 6.26"水滴全面屏，德国莱茵 TüV 认证护眼屏 / 18个月超长质保，康宁第五代大猩猩玻璃盖板 / 支持大字体、大音量模式，收音机、红外遥控功能 / 1200万AI双摄，1.25μm大像素相机',
      price_max: '1799',
      market_price_max: '1799',
      price_min: '699',
      market_price_min: '1799',
      value: '3GB+32GB',
      images: [
        'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/bd25cc614a670f4d5546fe82e239ef86.jpg?thumb=1&w=200&h=200&f=webp&q=90',
      ],
    },
    {
      count: 1,
      selected: true,
      product_id: 10000135,
      name: '小米9 SE',
      title: '小米9',
      product_desc_ext:
        '索尼4800万超广角三摄 / 骁龙712全球首发 / 全息幻彩机身 / 三星AMOLED屏幕 / 5.97”水滴屏 / 第五代屏幕指纹识别 / 多功能NFC / 红外遥控',
      price_max: '1599',
      market_price_max: '1599',
      price_min: '1599',
      market_price_min: '1599',
      value: '6GB+64GB',
      images: [
        '//i8.mifile.cn/a1/pms_1550646262.14555118.jpg',
        '//i8.mifile.cn/a1/pms_1550646262.13317112.jpg',
        '//i8.mifile.cn/a1/pms_1550646262.13052238.jpg',
        '//i8.mifile.cn/a1/pms_1550646262.14867742.jpg',
      ],
    },
  ],
}

const mutations = {
  // 数据中没有则添加商品和数量，有商品则添加数量
  ADDSHOPITEMMUTATION(state, good) {
    const shopItem = state.cartList.find((item) => {
      return item.product_id === good.product_id
    })
    if (shopItem) {
      shopItem.count += 1
    } else {
      state.cartList.push(good)

      let newArr = state.cartList.filter((item) => {
        if (item.product_id) {
          return true
        }
      })
      state.cartList = newArr

      Vue.set(good, 'count', 1)
      Vue.set(good, 'selected', true)
    }
  },
  // 改变单选按钮
  CHANGEISSELECTED(state, { selected, id }) {
    state.cartList.forEach((item) => {
      if (item.product_id === id) {
        item.selected = selected
      }
    })
  },
  // 点击删除 单个商品
  DELETEGOODS(state, id) {
    state.cartList = state.cartList.filter((item) => item.product_id !== id)
  },
  // 点击全选
  SELETEDALLGOODS(state, selected) {
    state.cartList.forEach((item) => (item.selected = selected))
  },
}
const actions = {}
const getters = {
  // 全选按钮状态
  changeSelectedAll(state) {
    return (
      state.cartList.length &&
      state.cartList.every((item) => {
        return item.selected
      })
    )
  },
}
export default {
  state,
  mutations,
  actions,
  getters,
}
